<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue'
import { artGetChannelService, artDelChannelService } from '@/api/article'
const channelList = ref([])
const loading = ref(false) // 表格加载效果默认没有
const dialog = ref() // 获取弹窗组件
// 页面加载获取分类
const getChannelList = async () => {
  loading.value = true  // 数据开始加载，效果打开
  const res = await artGetChannelService()
  channelList.value = res.data.data
  // console.log(channelList.value)
  loading.value = false // 数据加载完，效果关闭
}
getChannelList()
// 点击添加分类
const onAddChannel = () => {
  dialog.value.open({})
}
// 点击编辑
const onEditChannel = (row, $index) => {
  // console.log(row, $index)
  dialog.value.open(row)
}
// 点击删除
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('确定要删除该分类吗？','温馨提示',{
    type: 'warning',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
  })
  
  // console.log(row, $index)
  await artDelChannelService(row.id)
  ElMessage.success('删除成功')
  getChannelList() // 重新渲染页面
}
// 父组件操作
const onSuccess = () => {
  getChannelList() // 弹窗修改成功，重新渲染页面
} 
</script>

<template>
  <PageContainer title="文章分类">
    <!--添加按钮 用具名插槽-->
    <template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>
    <!--内容主体部分是表格 type="index"是索引号下标 v-loading="true"是有加载效果-->
    <el-table class="e-table" v-loading="loading" :data="channelList" style="width: 100%;">
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="cate_name" label="分类名称"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column label="操作" width="100">
        <!--row就是channelList的一项，$index下标-->
        <template #default="{row,$index}">
          <el-button circle type="primary" :icon="Edit" @click="onEditChannel(row,$index)" style="padding-left: 13px;"><!--编辑--></el-button>
          <el-button circle type="danger" :icon="Delete" @click="onDelChannel(row,$index)" style="padding-left: 13px;"><!--删除--></el-button>
        </template>
      </el-table-column>
      <!--如果内容为空-->
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <!--弹窗 :before-close="handleClose"关闭事件-->
    <ChannelEdit ref="dialog" :success="onSuccess"></ChannelEdit>
  </PageContainer>
</template>

<style lang="scss" scoped>
</style>
